import RewardItem from "./rewarditem";
import {utils} from "../utils/utils";
import {model} from "../web/model/Model";
import {wxUtils} from "../wx/wxUtils";
import {apiUtils} from "../web/api/ApiUtils";
import * as config from "../config/config";
import {WordsUtils} from "../utils/wordsUtils";

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewbieGift extends cc.Component {

  @property(cc.Label)
  num: cc.Label = null;

  @property(cc.Prefab)
  rewardItem: cc.Prefab = null;

  @property(cc.Node)
  rewardContainer: cc.Node = null;

  start() {
    let bg = this.node.getChildByName('bg');
    utils.dialogPopup(this.node, bg);
    let block = this.node.getChildByName('block');  // 屏蔽点击的全屏黑色半透背景
    let content = bg.getChildByName('content');  // 点击content外部关闭，bg的空白像素范围有点大
    block.on(cc.Node.EventType.TOUCH_END, (event: cc.Event.EventTouch) => {
      let p = content.convertTouchToNodeSpace(event.touch);
      if (p.x < 0 || p.x > content.width || p.y < 0 || p.y > content.height) {
        this.close();
      }
    }, this);

    // let aim = config.getGift(config.NEWBIEGIFT, 'aims');
    // let desc = `将夏日大作战分享到群即可领取丰厚大礼包`;
    // bg.getChildByName('desc').getComponent(cc.Label).string = desc;

    this._initReward();
    this._setState();
    model.user.on('newbieGift', this._setState, this);
  }

  private _initReward() {
    let container = this.rewardContainer;
    let startx = container.x - container.width / 2;
    let rewards = config.getGift(config.NEWBIEGIFT, 'reward');
    if (rewards.length <= 0) {
      return;
    }
    let w = container.width / rewards.length;
    rewards.forEach((rewardID, ix) => {
      let item = cc.instantiate(this.rewardItem);
      item.x = startx + w * (ix + 0.5);
      item.y = container.y;
      item.getComponent(RewardItem).init(rewardID);
      item.parent = container.parent;
    });
  }

  private _setState() {
    let aim = config.getGift(config.NEWBIEGIFT, 'aims');
    // 已领取，当前值设置为目标值
    let cur = model.user.newbieGiftValid ? Math.min(model.user.newbieGift, aim) : aim;
    this.num.string = `${cur}/${aim}`;
    let btnNode = this.node.getChildByName('bg').getChildByName('button');
    let btnText = btnNode.getChildByName('Label').getComponent(cc.Label);
    if (model.user.newbieGiftValid) {
      btnText.string = model.user.hasNewbieGiftReward ? '领取奖励' : '分享到群';
    } else {
      btnText.string = '已领取';
      btnNode.getComponent(cc.Button).interactable = false;
      btnText.node.getComponent(cc.LabelOutline).color = cc.color(120, 120, 120);
    }
  }

  async invite() {
    if (!model.user.newbieGiftValid) {
      return;
    }
    if (model.user.hasNewbieGiftReward) {
      // 领取奖励
      model.commonResult.once('0', () => {
        model.user.newbieGift = -99;
      }, this);
      apiUtils.getNewbieGift();
    } else {
      const res: any = await wxUtils.shareToFriends();
      if (res) {
        if (res.shareTickets) {
          WordsUtils.success('分享成功')
          apiUtils.shareNotify();
          if (model.user.newbieGiftValid) {
            model.user.newbieGift = model.user.newbieGift + 1;
          }
        } else {
          WordsUtils.warn('分享到群才可以')
        }
      } else {
        WordsUtils.error('分享失败')
      }
    }
  }

  close() {
    utils.dialogClose(this.node, this.node.getChildByName('bg'));
    wxUtils.showMenuAd()
  }

  onDestroy() {
    model.user.off('newbieGift', this._setState, this);
  }
}
